#include <iostream>
#include <fstream>
#include <math.h>
#include <exception>
#include <string>
#include "func.h"
#include "parm.h"
using namespace std;

int main(int argc, char** argv){
	//a0,rin,rout,用于计算角速度
    	//团块环向宽度dphcl,
    	//输入文件名filename_in,输出文件名filename_out
	//输出 光变曲线
	double a0 = stod(argv[1]);
	double rin = stod(argv[2]);
	double rout = stod(argv[3]);
	double dphcl = stod(argv[4]);
    	const int t_resolution = stod(argv[5]);
    	double filter_min = stod(argv[6]);
    	double filter_max = stod(argv[7]);
	ifstream input(argv[8]);
    	ofstream output(argv[9]); 
    	//计算相关变量
	double z1 = 1 + pow(1 - a0 * a0, 1.0 / 3.0) * (pow(1 + a0, 1.0 / 3.0) + pow(1 - a0, 1.0 / 3.0));
	double z2 = sqrt(3 * a0 * a0 + z1 * z1);
	double r_min = 3 + z2 - sqrt((3 - z1) * (3 + z1 + 2 * z2)) + 0.001;
	double rcl0 = 0.5 * (rin + rout);
	double omg = omega(a0, rcl0);
	double period = 2 * Pi / omg;
	double dt = period / t_resolution;
	double rclp = pow(rcl0, p0);
	dphcl = dphcl * Pi / 180;
    	double *flux = new double[t_resolution] {};
	try{
		while (true){
			double data[data_len] = {};
			for (int i = 0; i < data_len; ++i) 
				if (!(input >> data[i])) throw runtime_error("read over");
			const double phi1 = phiem(data[xt], data[yt], data[sr], data[stheta]);
			//原团块对应的时间
			double time_origin_end = phi1 / omg;
			//像对应的时间
			double time_image_end = time_origin_end + data[str] + data[stth];
			double time_image_begin = time_image_end - dphcl / omg;
            		//归一化到一个周期中
			int t_begin = normalization_time(time_image_begin, period, t_resolution);
			int t_end = normalization_time(time_image_end, period, t_resolution);
			//计算能段和强度
			int ji = round(elin * data[gt] * 10);
			double sg = data[ds] * pow(data[gt], 3) / rclp;
			//可用前缀和算法来减少插入操作的时间复杂度，不过本来程序运行速度也不慢
            		if(ji >= filter_min * 10 && ji <= filter_max * 10){
                		for (int t = t_begin; t != (t_end + 1) % t_resolution; t = (t + 1) % t_resolution){
					flux[t] += sg;
				}
           		}
		}
	}
	catch (runtime_error err){
		input.close();
	}
	output << dt << endl;
	for(int i = 0; i < t_resolution; ++ i){
        	output << flux[i] << ' ';
    	}
    	output << endl;
	output.close();
	return 0;
}

